package com.qwt.part_time_api.handler;

import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.utils.JsonAuthentication;
import com.qwt.part_time_api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限不足处理逻辑
 *
 * @author 君子慎独
 */

@Component
@Slf4j
public class LocalAccessDeniedHandler extends JsonAuthentication implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        log.info("权限不足");
        Result result = Result.fail(ErrorCode.ERR_NO_PERMISSION.getErrCode(), ErrorCode.ERR_NO_PERMISSION.getErrMsg());

        this.writeJson(request, response, result);
    }
}


